clear all
set more off, perm
capture log close
set maxvar 32767
global klmChinaReach2: env klmChinaReach2
global datapath "${klmChinaReach2}/Fuyao_Erlfang"
cd "$datapath\Eleanor\Control Groups"

use controlgroup_fw, clear
merge 1:1 ts_id using controlgroup, nogen keepusing(level* repeat*)

gen ub_old=mean_old+1.65*se_old
gen lb_old=mean_old-1.65*se_old
gen ub_new=mean_new+1.65*se_new
gen lb_new=mean_new-1.65*se_new

label var mean_old "Enrolled > 1 Month when the task was evaluated"
label var mean_new "Enrolled < 1 Month when the task was evaluated"
label var ub_old "90% Confidence Interval"
label var ub_new "90% Confidence Interval"

keep if N_old>10 & N_new>10

unab skills : level*
local skills : subinstr local skills "level" "", all
foreach skill of local skills {
	sort level`skill' ts_id
	gen index`skill'= sum(!missing(level`skill')) if !missing(level`skill')
}
unab skills : level*
local skills : subinstr local skills "level" "*", all
order `skills', after(max_age)
sort ts_id
save controlgroup_figures, replace

unab skills : level*
local skills : subinstr local skills "level" "", all
foreach skill of local skills {
	local xlines ""
	local xlabels ""
	sum level`skill'
	if r(N)>0 {
	forval level=`r(min)'/`r(max)' {
		sum index`skill' if level`skill'==`level'
		if r(N)>0 {
		local xlines "`xlines' `r(max)'"
		local xlabels `" `xlabels' `r(max)' "`level'" "'
		}
	}
	sort index`skill'
	if inlist("`skill'", "f2") ==1 {
	twoway line mean_old index`skill', sort lwidth(medthin) || line ub_old index`skill', sort lcolor(gs15) lpattern(dash) lwidth(medthin) || line lb_old index`skill', sort lcolor(gs15) lpattern(dash) lwidth(medthin) || connected mean_new index`skill', sort msize(small) lwidth(medthin) || line ub_new index`skill', sort lcolor(gs15) lpattern(dash) lwidth(medthin) || line lb_new index`skill', sort lcolor(gs15) lpattern(dash) lwidth(medthin) ///
	xline(`xlines', lcolor(khaki) lpattern(dash) lwidth(thin)) xlabel(`xlabels', labsize(small)) xmtick(, labsize(small)) ///
	xtitle(Difficulty Level, size(small)) ytitle(Passing Rate, size(small)) ylabel(,labsize(small)) ///
	title("Average Passing Rate for Fine Motor Tasks by Length of Enrollment", size(med)) legend(order(1 2 4) size(vsmall) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("1. Enrolled < 1 Month represents children who had been in the program for less than one month when the task was evaluated." "2. Enrolled > 1 Month represents children who had been in the program for more than one month when the task was evaluated, who continued" "to stay in the program for 2 years.  3. 90% confidence intervals are shown for both groups." "4. Tasks with fewer than 10 observations in either group are omitted.   5. The dashed yellow lines indicate the last task at each difficulty level." "Within difficulty levels, tasks are arranged by order of the children taking them.", size(vsmall) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) ///
	saving("sum_figures/controlgroup_`skill'", replace)
	graph export "sum_figures/controlgroup_`skill'.pdf", replace	
	}
	else if inlist("`skill'","l")==1 {
	twoway line mean_old index`skill', sort lwidth(medthin) || line ub_old index`skill', sort lcolor(gs15) lpattern(dash) lwidth(medthin) || line lb_old index`skill', sort lcolor(gs15) lpattern(dash) lwidth(medthin) || connected mean_new index`skill', sort msize(small) lwidth(medthin) || line ub_new index`skill', sort lcolor(gs15) lpattern(dash) lwidth(medthin) || line lb_new index`skill', sort lcolor(gs15) lpattern(dash) lwidth(medthin) ///
	xline(`xlines', lcolor(khaki) lpattern(dash) lwidth(thin)) xlabel(`xlabels', labsize(small)) xmtick(, labsize(small)) ///
	xtitle(Difficulty Level, size(small)) ytitle(Passing Rate, size(small)) ylabel(,labsize(small)) ///
	title("Average Passing Rate for Language Tasks by Length of Enrollment", size(med)) legend(order(1 2 4) size(vsmall) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("1. Enrolled < 1 Month represents children who had been in the program for less than one month when the task was evaluated." "2. Enrolled > 1 Month represents children who had been in the program for more than one month when the task was evaluated, who continued" "to stay in the program for 2 years.  3. 90% confidence intervals are shown for both groups." "4. Tasks with fewer than 10 observations in either group are omitted.   5. The dashed yellow lines indicate the last task at each difficulty level." "Within difficulty levels, tasks are arranged by order of the children taking them.", size(vsmall) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) ///
	saving("sum_figures/controlgroup_`skill'", replace)
	graph export "sum_figures/controlgroup_`skill'.pdf", replace	
	}
	else if inlist("`skill'", "c2")==1 {
	twoway line mean_old index`skill', sort lwidth(medthin) || line ub_old index`skill', sort lcolor(gs15) lpattern(dash) lwidth(medthin) || line lb_old index`skill', sort lcolor(gs15) lpattern(dash) lwidth(medthin) || connected mean_new index`skill', sort msize(small) lwidth(medthin) || line ub_new index`skill', sort lcolor(gs15) lpattern(dash) lwidth(medthin) || line lb_new index`skill', sort lcolor(gs15) lpattern(dash) lwidth(medthin) ///
	xline(`xlines', lcolor(khaki) lpattern(dash) lwidth(thin)) xlabel(`xlabels', labsize(small)) xmtick(, labsize(small)) ///
	xtitle(Difficulty Level, size(small)) ytitle(Passing Rate, size(small)) ylabel(,labsize(small)) ///
	title("Average Passing Rate for Cognitive Tasks by Length of Enrollment", size(med)) legend(order(1 2 4) size(vsmall) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("1. Enrolled < 1 Month represents children who had been in the program for less than one month when the task was evaluated." "2. Enrolled > 1 Month represents children who had been in the program for more than one month when the task was evaluated, who continued" "to stay in the program for 2 years.  3. 90% confidence intervals are shown for both groups." "4. Tasks with fewer than 10 observations in either group are omitted.   5. The dashed yellow lines indicate the last task at each difficulty level." "Within difficulty levels, tasks are arranged by order of the children taking them.", size(vsmall) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) ///
	saving("sum_figures/controlgroup_`skill'", replace)
	graph export "sum_figures/controlgroup_`skill'.pdf", replace	
	}
	else if inlist("`skill'", "g")==1 {
	twoway line mean_old index`skill', sort lwidth(medthin) || line ub_old index`skill', sort lcolor(gs15) lpattern(dash) lwidth(medthin) || line lb_old index`skill', sort lcolor(gs15) lpattern(dash) lwidth(medthin) || connected mean_new index`skill', sort msize(small) lwidth(medthin) || line ub_new index`skill', sort lcolor(gs15) lpattern(dash) lwidth(medthin) || line lb_new index`skill', sort lcolor(gs15) lpattern(dash) lwidth(medthin) ///
	xline(`xlines', lcolor(khaki) lpattern(dash) lwidth(thin)) xlabel(`xlabels', labsize(small)) xmtick(, labsize(small)) ///
	xtitle(Difficulty Level, size(small)) ytitle(Passing Rate, size(small)) ylabel(,labsize(small)) ///
	title("Average Passing Rate for Gross Motor Tasks by Length of Enrollment", size(med)) legend(order(1 2 4) size(vsmall) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("1. Enrolled < 1 Month represents children who had been in the program for less than one month when the task was evaluated." "2. Enrolled > 1 Month represents children who had been in the program for more than one month when the task was evaluated, who continued" "to stay in the program for 2 years.  3. 90% confidence intervals are shown for both groups." "4. Tasks with fewer than 10 observations in either group are omitted.   5. The dashed yellow lines indicate the last task at each difficulty level." "Within difficulty levels, tasks are arranged by order of the children taking them.", size(vsmall) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) ///
	saving("sum_figures/controlgroup_`skill'", replace)
	graph export "sum_figures/controlgroup_`skill'.pdf", replace	
	}
	}
}



foreach s in f2 c2 l g{
	twoway line mean_old week_age if level`s' != ., sort lwidth(medthin) || line ub_old week_age if level`s' != ., sort lcolor(gs15) lpattern(dash) lwidth(medthin) || line lb_old week_age if level`s' != ., sort lcolor(gs15) lpattern(dash) lwidth(medthin) || connected mean_new week_age if level`s' != ., sort msize(small) lwidth(medthin) || line ub_new week_age if level`s' != ., sort lcolor(gs15) lpattern(dash) lwidth(medthin) || line lb_new week_age if level`s' != ., sort lcolor(gs15) lpattern(dash) lwidth(medthin) ///
	xmtick(, labsize(small)) xlabel(minmax, labsize(small))  ///
	xtitle(Weekly Age, size(small)) ylabel(, labsize(small)) ymtick(, labsize(small)) ytitle(Passing Rate, size(small)) ///
	title("Average Passing Rate for `s' Tasks by Length of Enrollment ", size(medium)) ///
	legend(order(1 2 4) size(vsmall) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero))  ///
	note("1. Enrolled < 1 Month represents children who had been in the program for less than one month when the task was evaluated." "2. Enrolled > 1 Month represents children who had been in the program for more than one month when the task was evaluated, who continued" "to stay in the program for 2 years." "3. 90% confidence intervals are shown for both groups." "4. Tasks with fewer than 10 observations in either group are omitted.", size(vsmall) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) ///
	saving("sum_figures/week_age_`s'", replace)
}


// 	twoway line mean_old week_age if level`s' != ., sort lwidth(medthin) || line ub_old week_age if level`s' != ., sort lcolor(ltblue) lpattern(dash) lwidth(medthin) || line lb_old week_age if level`s' != ., sort lcolor(ltblue) lpattern(dash) lwidth(medthin) || line mean_new week_age if level`s' != ., sort lpattern(longdash_shortdash) lwidth(medthin) ///
